#encoding=utf-8
'''
Created on 2011-12-7

@author: guanyw

python version:2.6
'''
from basic import data_floder_name, rule_file, cnf_rule_file
import nltk
import threading
#===============================================================================
# MultipleThreadRunner
#===============================================================================
def MultipleThreadRunner(progrram, task_list,other_args, threadCnt=4):
    thread_pool = []
    taskCntEach = int(len(task_list) / threadCnt) + 1
    for i in range(threadCnt): 
        if (i + 1) * taskCntEach >= len(task_list):
            th = threading.Thread(target=progrram, args=(task_list[i * taskCntEach:], other_args,i))
            thread_pool.append(th) 
            threadCnt = i+1
            break
        else: 
            th = threading.Thread(target=progrram, args=(task_list[i * taskCntEach:(i + 1) * taskCntEach], other_args,i)) 
        thread_pool.append(th) 
         
    #-------------------------------------------------  start threads one by one
    for i in range(threadCnt): 
        thread_pool[i].start() 
     
    #------------------------------------------------------- collect all threads
    for i in range(threadCnt): 
        threading.Thread.join(thread_pool[i]) 



def get_all_ternimals():
    grammer = nltk.parse_cfg(open(rule_file).read())
    nonterminals = set([str(t.lhs()) for t in grammer.productions()])
    f = open('nonterminals.txt','w')
    f.write('\n'.join(nonterminals))
    
def chomsky_normal_form():
    grammer = nltk.parse_cfg(open(rule_file).read())
    nonterminals = set([t.lhs() for t in grammer.productions()])
    f = open('nonterminals.txt','w')
    f.write(','.join(nonterminals))
    print nonterminals
    def has_terminal(rhs): 
        for r in rhs:
            if r not in nonterminals:
                return True
        return False
    
    for rule in grammer.productions():
        lhs = rule.lhs()
        rhs = rule.rhs()
        rhs_len = len(rhs)
        if rhs_len == 1:
            if has_terminal(rhs):
                print rule
                
            
if __name__ == '__main__':
    rules = open(rule_file).read()
    grammer = nltk.parse_cfg(rules)
    grammer.nonterminals = set([t.lhs() for t in grammer.productions()])
    